﻿using System;
using System.IO;
using System.Net;
using System.Web;

namespace WebMsg
{
    public class NetOnboard : WebMsg
    {
        public NetOnboard()
            : base()
        {
            this.messageURL = @"http://gateway.netonboard.com:81/cpm_i.aspx";
            this.balanceURL = @"http://gateway.netonboard.com:81/bulkcheck.aspx";
            this.CustomFrom = true;
        }

        protected override void AddResults()
        {
        }

        protected override String SendMessage(Sms message)
        {
            if (message == null)
                throw new ArgumentNullException("message");

            HttpWebRequest _Req2SendSMS = (HttpWebRequest)System.Net.HttpWebRequest.Create(this.messageURL);
            {
                _Req2SendSMS.Headers.Add("trx_id", HttpUtility.UrlEncode("0"));
                _Req2SendSMS.Headers.Add("passname", HttpUtility.UrlEncode(User));
                _Req2SendSMS.Headers.Add("password", HttpUtility.UrlEncode(Pass));
                _Req2SendSMS.Headers.Add("short_code", HttpUtility.UrlEncode("36828"));
                _Req2SendSMS.Headers.Add("originating_no", HttpUtility.UrlEncode(message.From.Number));
                _Req2SendSMS.Headers.Add("destination_no", HttpUtility.UrlEncode(message.To.Number));
                _Req2SendSMS.Headers.Add("cp_ref_id", HttpUtility.UrlEncode("0"));
                _Req2SendSMS.Headers.Add("bill_type", HttpUtility.UrlEncode("0"));
                _Req2SendSMS.Headers.Add("bill_price", HttpUtility.UrlEncode("0"));
                _Req2SendSMS.Headers.Add("content_type", HttpUtility.UrlEncode("1"));
                _Req2SendSMS.Headers.Add("msg", HttpUtility.UrlEncode(message.Body));
                _Req2SendSMS.Headers.Add("bulk_fg", HttpUtility.UrlEncode("1"));
            }

            HttpWebResponse myWebResponse = (HttpWebResponse)_Req2SendSMS.GetResponse();
            WebHeaderCollection myWebHeaderCollection = myWebResponse.Headers;

            myWebResponse.GetResponseStream();
          
            return myWebHeaderCollection["result"];
        }

        protected override ResultCodes ParseResult(String raw)
        {
            return base.ParseResult(raw);
        }

        public override String GetCreditsRemaining()
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(this.balanceURL);

            request.Headers.Add("passname", User);
            request.Headers.Add("password", Pass);

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            String balance = response.GetResponseHeader("credit");

            return balance;
        }
    }
}
